﻿using System;
using System.Collections.Generic;
using System.Text;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    /// <summary>
    /// 输出一个进度条
    /// </summary>
    public  class PageCommon_Progress:ControlBase
    {
        private int _maxValue = 100;
        private int _minValue = 0;
        private int _currentValue = 0;
        private ColorPaletteEnum _color = ColorPaletteEnum.success;
        private ProgressSizeEnum _size = ProgressSizeEnum.xs;
        private DirectionTypeEnum _direction = DirectionTypeEnum.Horizontal;

        public PageCommon_Progress(ViewContext viewContext) : base(viewContext)
        {
            this.Css("progress webui-ctrl-progress");
        }

        /// <summary>
        /// 进度条最大值
        /// </summary>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public PageCommon_Progress MaxValue(int maxValue)
        {
            _maxValue = maxValue;
            return this;
        }

        /// <summary>
        /// 进度条最小值
        /// </summary>
        /// <param name="minValue"></param>
        /// <returns></returns>
        public PageCommon_Progress MinValue(int minValue)
        {
            _minValue = minValue;

            return this;
        }

        /// <summary>
        /// 进度条当前值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public PageCommon_Progress CurrentValue(int value)
        {
            _currentValue = value;

            return this;
        }

        /// <summary>
        /// 进度条的颜色
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public PageCommon_Progress Color(ColorPaletteEnum color)
        {
            _color = color;

            return this;
        }

        /// <summary>
        /// 进度条的大小,默认是 xs
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageCommon_Progress Size(ProgressSizeEnum size)
        {
            _size = size;

            return this;
        }

        /// <summary>
        /// 滚动条方向
        /// </summary>
        /// <param name="direction"></param>
        /// <returns></returns>
        public PageCommon_Progress Direction(DirectionTypeEnum direction)
        {
            _direction = direction;
            return this;
        }

        public override HtmlString Render()
        {
            /*
              <div class="progress">
                <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 40%">
                  <span class="sr-only">40% Complete (success)</span>
                </div>
              </div>
             */

            var percent = (1.0 * _currentValue / _maxValue) * 100;

            this.Css("progress-" + _size);

            if (_direction== DirectionTypeEnum.Vertical)
            {
                this.Css("vertical");
            }

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }


            ViewContext.Writer.WriteLine($"<div {Attributes}>");
            ViewContext.Writer.WriteLine($"<div class=\"progress-bar progress-bar-{_color} progress-bar-striped\" role=\"progressbar\" aria-valuenow=\"{_currentValue}\" aria-valuemin=\"{_minValue}\" aria-valuemax=\"{_maxValue}\" style=\"width: {percent}%\">");
            ViewContext.Writer.WriteLine($"<span class=\"sr-only\">{percent}% Complete</span>");
            ViewContext.Writer.WriteLine("</div> <!-- .progress-bar -->");
            ViewContext.Writer.WriteLine("</div> <!-- .progress -->");

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }

            return HtmlString.Empty;
        }
    }
}
